
/*
    Image Viewer.
    v0.1.1:         加入移动图片功能
    0.1.2:          加入设置缩放比功能
    0.1.3:          修改部分按钮配色
    0.1.4:          Idle_Text, if hasnot open pic, ban zoom
    0.1.5-alpha
    1.0-pre1.       加入字体设置界面
    1.0-pre2.       加入进入字体设置的按钮
    1.0-release.
    1.1             读取图片时自动将过大图片缩小至合适, 增大移动图片的速度
    1.2             加入模糊功能
    1.3
    1.4(5)          更新图像模糊算法, 适应更大的sigma值, 修复σ过大时图像变暗的bug
    1.5(10)         加入图像加密算法
    1.6             加入保存图片功能, 可读取本程序保存的加密图片
*/

#ifndef UNICODE
#define UNICODE
#endif // UNICODE
#ifndef _UNICODE
#define _UNICODE
#endif // _UNICODE
#include <seg.hpp>
#pragma comment(lib, "Comdlg32.lib")
#include <commdlg.h>
#define IVVER L"1.6.2"
#define WT_IVVER L"Image Viewer "  IVVER
#define CallBack int
// cl ImageViewer.cpp /utf-8 /I"E:\Environments\cl\seg" /EHsc

constexpr int Width     = 1660;
constexpr int Height    = 930;
constexpr int Movespeed = 30;

namespace SetFontWS
{
    /*
    选择字体: xxx
    设置字号: yyy
    Exit
    */
    String font_now;
    int font_ssize;
    seg::Interface scr(5, RGB(252, 252, 252), true);
    void inits();
    CallBack sure(seg::ClickableObject &self);
} // namespace SetFontWS

void SetFontWS::inits()
{
    SetFontWS::font_now.set(L"Default");
    SetFontWS::font_ssize = 0;
    SetFontWS::scr.cbs[0].init(
        500, 269, 169, 96, 4, 4, RGB(64, 10, 96), RGB(255, 240, 255), true, 0, false, false
    );
    SetFontWS::scr.cbs[0].s.set(L"选择字体\n(simhei,consolas,...)");
    SetFontWS::scr.cbs[2].init(
        500, 569, 169, 96, 4, 4, RGB(64, 10, 96), RGB(255, 240, 255), true, 2, false, false
    );
    SetFontWS::scr.cbs[2].s.set(L"选择字体大小");
    SetFontWS::scr.cbs[1].init(
        1080, 269, 169, 96,  3,  5, RGB(0xc5, 0xfb, 0x46), RGB(255, 225, 255), true, 1, true, false
    );
    SetFontWS::scr.cbs[1].s.set(L"Default");
    SetFontWS::scr.cbs[3].init(
        1080, 569, 169, 96,  3,  5, RGB(0xc5, 0xfb, 0x46), RGB(255, 225, 255), true, 3, true, false
    );
    SetFontWS::scr.cbs[3].s.set(L"16");
    SetFontWS::scr.cbs[4].init(
        840, 769, 169, 96, 5, 6, RGB(32, 96, 96), RGB(255, 240, 255), true, 4, false, false
    );
    SetFontWS::scr.cbs[4].s.set(L"确定");
    SetFontWS::scr.cbs[4].OnClick = SetFontWS::sure;
    LOGFONT f;
    gettextstyle(&f);
    SetFontWS::font_ssize = f.lfHeight;
    f.lfQuality = ANTIALIASED_QUALITY;		// 设置输出效果为抗锯齿
    settextstyle(&f);
}


CallBack SetFontWS::sure(seg::ClickableObject & self)
{
    if (SetFontWS::scr.cbs[3].s.isint() == false)
    {
        MessageBoxW(seg::MainHandle, L"字体大小必须为整数", WT_IVVER, MB_OK);
        return 0;
    }
    swscanf(SetFontWS::scr.cbs[3].s.get(), L"%d", &SetFontWS::font_ssize);
    LOGFONT f;
    gettextstyle(&f);
    f.lfHeight = SetFontWS::font_ssize;
    _tcscpy(f.lfFaceName, SetFontWS::scr.cbs[1].s.get());
    settextstyle(&f);
    return true;
}

namespace Show_IMG
{
    /*
    Open Amplify Reduce X-Flip Y-Flip Exit
    IMAGE*/
    seg::Interface screen(22, RGB(252, 252, 252), false);
    seg::Pillow::PIC img(NULL, -1);
    seg::Pillow::PIC Image_old(NULL, -1);
    String Idle_Text;
    bool HadOpened = false;
    double ZR = 1.1; // Zoom rate p
    double Zoomrate = 1; // this *= ZR
    int IMG_MinSize = 50;
    // 偏移
    int dx = 0;
    int dy = 0;
    // info
    wchar_t ImgPath[260];
    String ImageInfo;
    CallBack Open(seg::ClickableObject &self);
    CallBack Amplify(seg::ClickableObject &self);
    CallBack Reduce (seg::ClickableObject& self);
    CallBack XFlip(seg::ClickableObject & self);
    CallBack YFlip(seg::ClickableObject  & self);
    CallBack ExitMainloop(seg::ClickableObject &self){return true;}
    CallBack SetZoomRate(seg::ClickableObject &self);
    CallBack Up(seg::ClickableObject& self);
    CallBack Down(seg::ClickableObject &self);
    CallBack Left(seg::ClickableObject &self);
    CallBack Right(seg::ClickableObject &self);
    CallBack inter_setfont(seg::ClickableObject &self){SetFontWS::scr.mainloop();return false;}
    CallBack RawSmooth(seg::ClickableObject & self);
    CallBack BackImg(seg::ClickableObject &self);
    CallBack Enc(seg::ClickableObject & self);
    CallBack Dec(seg::ClickableObject & self);
    CallBack SwitchDebug(seg::ClickableObject & self);
    CallBack ShowImageInfo(seg::ClickableObject &self);
    CallBack Save(seg::ClickableObject &self);
    void render_img(seg::ClickableObject &self);
    int w_main();
    int w_main(String &);
} // namespace Show_IMG

CallBack Show_IMG::Open(seg::ClickableObject &self)
{
    TCHAR szPath[MAX_PATH]{ 0 };
	TCHAR szTitle[MAX_PATH]{ 0 };
	TCHAR szFilePath[MAX_PATH]{ 0 };
	TCHAR szFiles[2*MAX_PATH]{ 0 };
	TCHAR *p;
	LPOPENFILENAMEW var = new OPENFILENAMEW;
	ZeroMemory(var, sizeof(OPENFILENAMEW));
	var->hwndOwner = seg::MainHandle;
	var->lStructSize = sizeof(OPENFILENAMEW);
	var->lpstrTitle = WT_IVVER;
	var->lpstrFilter =  L"图片(*.jpg,*.jpeg,*.png,*.ive)\0*.jpg;*.png;*.jpeg;*.ive\0所有文件(*.*)\0*.*\0\0";
	var->lpstrInitialDir = L".\\picpic";
	var->lpstrFile = (LPWSTR)szFiles;
	var->nMaxFile = sizeof(szFiles);
	var->nFilterIndex = NULL;
	var->lpstrFileTitle = (LPWSTR)szTitle;
	var->Flags =
		OFN_PATHMUSTEXIST | //只允许输入存在路径
		OFN_FILEMUSTEXIST | //只允许输入存在文件
		OFN_EXPLORER | 		//使用Explorer风格的默认对话框的子窗口作为指定模板来建立一个对话框
		OFN_NOCHANGEDIR;// | 	//不变化当前路径，以防止影响文件操作
		//OFN_ALLOWMULTISELECT;	//允许多选
	if (GetOpenFileNameW(var)) {
        Show_IMG::img.load(var->lpstrFile, true);
        swprintf(Show_IMG::ImgPath, L"%s", var->lpstrFile);
        if (Show_IMG::img.img.getheight() >= Height - 20  ||  Show_IMG::img.img.getwidth() >= Width - 5)
        {
            double zmrt = 0.9;
            while (Show_IMG::img.img.getheight() * zmrt >= Height - 20  ||  Show_IMG::img.img.getwidth() * zmrt >= Width - 5)
            {
                zmrt *= 0.9;
            }
            Show_IMG::img.scale(zmrt, true);
        }
        Show_IMG::Image_old.img = Show_IMG::img.img;
        Show_IMG::HadOpened = true;
	}else
	MessageBoxW(seg::MainHandle, L"没有文件", L"tip", MB_OK);
	delete var;
    return 0;
}


CallBack Show_IMG::Reduce(seg::ClickableObject &self)
{
    if (Show_IMG::HadOpened == false)
    {
        MessageBoxW(seg::MainHandle, L"你还没有打开图片!", WT_IVVER, MB_OK);
        return 0;
    }
    if (Show_IMG::img.img.getwidth() < Show_IMG::IMG_MinSize || Show_IMG::img.img.getheight() < Show_IMG::IMG_MinSize)
    {
        MessageBoxW(seg::MainHandle, L"图片已缩至最小", WT_IVVER, MB_OK);
        return 0;
    }
    Show_IMG::Zoomrate /= Show_IMG::ZR;
    //Show_IMG::img.scale(Show_IMG::Smaller, true, 0);
    //Show_IMG::Image_old.scale((&(Show_IMG::img)), (&Show_IMG::Image_old), Show_IMG::Zoomrate, true, 0);
    Show_IMG::img.img = Show_IMG::Image_old.img;
    Show_IMG::img.scale(Show_IMG::Zoomrate, true, 0);
    return 0;
}
CallBack Show_IMG::Amplify(seg::ClickableObject &self)
{
    if (Show_IMG::HadOpened == false)
    {
        MessageBoxW(seg::MainHandle, L"你还没有打开图片!", WT_IVVER, MB_OK);
        return 0;
    }
    if (Show_IMG::img.img.getwidth() > 1650 || Show_IMG::img.img.getheight() > 920)
    {
        MessageBoxW(seg::MainHandle, L"图片已放至最大", WT_IVVER, MB_OK);
        return 0;
    }
    Show_IMG::Zoomrate *= Show_IMG::ZR;
    Show_IMG::img.img = Show_IMG::Image_old.img;
    Show_IMG::img.scale(Show_IMG::Zoomrate, true, 0);
    return 0;
}


CallBack Show_IMG::XFlip(seg::ClickableObject &self)
{
    if (Show_IMG::HadOpened == false)
    {
        MessageBoxW(seg::MainHandle, L"你还没有打开图片!", WT_IVVER, MB_OK);
        return 0;
    }
    IMAGE tmp(Show_IMG::img.img.getwidth(), Show_IMG::img.img.getheight());
    seg::Pillow::Flip(seg::Pillow::Directs::X, &(Show_IMG::img.img), &tmp);
    Show_IMG::img.img = tmp;
    return 0;
}

CallBack Show_IMG::YFlip(seg::ClickableObject &self)
{
    if (Show_IMG::HadOpened == false)
    {
        MessageBoxW(seg::MainHandle, L"你还没有打开图片!", WT_IVVER, MB_OK);
        return 0;
    }
    IMAGE tmp(Show_IMG::img.img.getwidth(), Show_IMG::img.img.getheight());
    seg::Pillow::Flip(seg::Pillow::Directs::Y, &(Show_IMG::img.img), &tmp);
    Show_IMG::img.img = tmp;
    return 0;
}

void Show_IMG::render_img(seg::ClickableObject &self)
{
    if (Show_IMG::HadOpened == true)
        Show_IMG::img.blit(self.x + Show_IMG::dx, self.y + Show_IMG::dy);
    else
        seg::render::multiline(
            int((self.x + self.x + self.width) / 2 ) + Show_IMG::dx,
            int((self.y + self.y + self.height) / 2 ) + Show_IMG::dy,
            Show_IMG::Idle_Text,
            169, seg::render::Place::lefttop
        );
}

CallBack Show_IMG::SetZoomRate(seg::ClickableObject &self)
{
    // TODO
    //MessageBoxW(seg::MainHandle, L"该功能尚未启用。", Show_IMG::screen.cbs[12].s.get(), MB_OK );
    if (Show_IMG::screen.cbs[12].s.isfloat())
    {
        swscanf(Show_IMG::screen.cbs[12].s.get(), L"%lf", &(Show_IMG::ZR));
        if (Show_IMG::ZR > 1 && Show_IMG::ZR <= 5)
        {
            MessageBoxW(seg::MainHandle, L"Successfully setting Zoom rate.", WT_IVVER, MB_OK);
        }
        else
        {
            MessageBoxW(seg::MainHandle, L"Zoom rate must in (1, 5].", WT_IVVER, MB_OK);
            Show_IMG::ZR = 1.1;
        }
    }
    else if (Show_IMG::screen.cbs[12].s.isint())
    {
        int tmp = 0;
        swscanf(Show_IMG::screen.cbs[12].s.get(), L"%d", &tmp);
        if (tmp > 1 && tmp <= 5)
        {
            Show_IMG::ZR = (double)tmp;
            MessageBoxW(seg::MainHandle, L"Successfully setting Zoom rate.", WT_IVVER, MB_OK);
        }
        else
        {
            MessageBoxW(seg::MainHandle, L"Zoom rate must in (1, 5].", WT_IVVER, MB_OK);
        }
    }
    return 0;
}

CallBack Show_IMG::RawSmooth(seg::ClickableObject & self)
{
    if (Show_IMG::HadOpened == false)
    {
        MessageBoxW(seg::MainHandle, L"你还没有打开图片!", WT_IVVER, MB_OK);
        return 0;
    }
    decimal_t sgm = 2.0;
    if (Show_IMG::screen.cbs[15].s.isfloat())
    {
        sgm = (decimal_t)Show_IMG::screen.cbs[15].s.cfloat();
    }
    else if (Show_IMG::screen.cbs[15].s.isint())
    {
        sgm = (decimal_t)Show_IMG::screen.cbs[15].s.cint();
    }
    else
    {
        MessageBoxW(seg::MainHandle, L"输入的sigma不正确。", WT_IVVER, MB_OK);
        return 0;
    }
    Show_IMG::dx = 0;
    Show_IMG::dy = 0;
    Show_IMG::img.img = AIP::GaussSmooth(Show_IMG::Image_old.img, sgm);
    MessageBoxW(seg::MainHandle, L"图像模糊处理完毕", WT_IVVER, MB_OK);
    return 0;
}

CallBack Show_IMG::BackImg(seg::ClickableObject & self)
{
    if (Show_IMG::HadOpened == false)
    {
        MessageBoxW(seg::MainHandle, L"你还没有打开图片!", WT_IVVER, MB_OK);
        return 0;
    }
    Show_IMG::img.img = Show_IMG::Image_old.img;
    MessageBoxW(seg::MainHandle, L"图像已还原", WT_IVVER, MB_OK);
    return 0;
}

CallBack Show_IMG::Enc(seg::ClickableObject & self)
{
    if (Show_IMG::HadOpened == false)
    {
        MessageBoxW(seg::MainHandle, L"你还没有打开图片!", WT_IVVER, MB_OK);
        return 0;
    }
    IMAGE tmp;
    tmp = sese::sepic4(Show_IMG::img.img);
    Show_IMG::img.img = tmp;
    MessageBoxW(seg::MainHandle, L"图像加密成功", WT_IVVER, MB_OK);
    return 0;
}

CallBack Show_IMG::Dec(seg::ClickableObject & self)
{
    if (Show_IMG::HadOpened == false)
    {
        MessageBoxW(seg::MainHandle, L"你还没有打开图片!", WT_IVVER, MB_OK);
        return 0;
    }
    IMAGE tmp;
    tmp = sese::resepic4(Show_IMG::img.img);
    Show_IMG ::img.img = tmp;
    MessageBoxW(seg::MainHandle, L"图像解密成功", WT_IVVER, MB_OK);
    return 0;
}

CallBack Show_IMG::SwitchDebug(seg::ClickableObject & self)
{
    Show_IMG::screen.ShowDebugInfo = !Show_IMG::screen.ShowDebugInfo;
    return 0;
}

CallBack Show_IMG::ShowImageInfo(seg::ClickableObject & self)
{
    if (Show_IMG::HadOpened == false)
    {
        MessageBoxW(seg::MainHandle, L"你还没有打开图片!", WT_IVVER, MB_OK);
        return 0;
    }
    Show_IMG::ImageInfo.set(Show_IMG::ImgPath);
    Show_IMG::ImageInfo.join(L"\nWidth: ");
    Show_IMG::ImageInfo.join(Show_IMG::img.img.getwidth());
    Show_IMG::ImageInfo.join(L"\nHeight:");
    Show_IMG::ImageInfo.join(Show_IMG::img.img.getheight());
    MessageBoxW(seg::MainHandle, Show_IMG::ImageInfo.get(), WT_IVVER, MB_OK);
    return 0;
}

CallBack Show_IMG::Save(seg::ClickableObject &self)
{
    if (Show_IMG::HadOpened == false)
    {
        MessageBoxW(seg::MainHandle, L"你还没有打开图片!", WT_IVVER, MB_OK);
        return 0;
    }
    wchar_t fname[100]{0};
    bool b = InputBox(fname, 99, L"请输入文件名", WT_IVVER, NULL, 0, 0, false);
    if (b == false)
    {
        return 0;
    }
    String s;
    s.set(fname);
    // no < > / \ | :  * ? 
    if (s.FindFrom(L"<>/\\|:*?\0"))
    {
        MessageBoxW(seg::MainHandle, L"文件名中有非法字符！", WT_IVVER, MB_OK);
        return 0;
    }
    String temp;
    temp = s.strip(L" .");
    if (temp.len() <= 0)
    {
        MessageBoxW(seg::MainHandle, L"文件名无效。", WT_IVVER, MB_OK);
        return 0;
    }
    if (temp.endswith(L".png") || temp.endswith(L".bmp") || temp.endswith(L".jpg") || temp.endswith(L"tif")
        || temp.endswith(L".gif")
    ) {
        pass;
    }
    else
    {
        temp.join(L".png");
    }
    wchar_t sss[280];
    swprintf(sss, L"图片将被保存至:\n    \".\\picpic\\%s\".\n是否确定? ", temp.get());
    int iresult = MessageBoxW(seg::MainHandle, sss, WT_IVVER, MB_OKCANCEL);
    if (iresult == IDOK)
    {
        swprintf(sss, L".\\picpic\\%s", temp.get());
        saveimage(sss, &(Show_IMG::img.img));
        return 0;
    }
    else
        pass
    return 0;
}

CallBack Show_IMG::Up(seg::ClickableObject &self)
{
    Show_IMG::dy -= Movespeed;
    return 0;
}

CallBack Show_IMG::Down(seg::ClickableObject &self)
{
    Show_IMG::dy += Movespeed;
    return 0;
}

CallBack Show_IMG::Left(seg::ClickableObject &self)
{
    Show_IMG::dx -= Movespeed;
    return 0;
}

CallBack Show_IMG::Right(seg::ClickableObject &self)
{
    Show_IMG::dx += Movespeed;
    return 0;
}

int Show_IMG::w_main()
{
    for (int i = 0; i < 11; i++)
    {
        if (i < 5)
        Show_IMG::screen.cbs[i].init
        (
            i * 90, 0, 82, 36, 2, 0, RGB(255, 169, 0), RGB(236, 249, 238), true, i, false, false,RGB(69,0,69)
        );
        else if (i == 5)
        Show_IMG::screen.cbs[i].init
        (
            1569, 0, 82, 36, 2, 0, RGB(255, 20, 0), RGB(220, 220, 255), true, i, false, false, RGB(255,20,0),PS_DASHDOT
        );
        else if (i > 6)
        Show_IMG::screen.cbs[i].init
        (
            (i - 2) * 90, 0, 82, 36, 3, 0, RGB(255, 196, 0), RGB(238, 236, 255), true, i, false, false
        );
    }
    Show_IMG::screen.cbs[11].init(
        1469, 42, 82, 32, 2, 0, RGB(255, 169, 0), RGB(236, 249, 238), true, 11, false, false,RGB(69,0,69)
    );
    Show_IMG::screen.cbs[12].init
    (
        1560, 42, 100, 32, 2, 3, RGB(255, 129, 69), RGB(210, 200, 255), true, 12, true, false
    );
    Show_IMG::screen.cbs[13].init
    (
        1469, 0, 82, 32, 2, 0, RGB(255, 169, 0), RGB(236, 249, 238), true, 13, false, false,RGB(69,0,69)
    );
    //Show_IMG::screen.cbs[14].init(
    //    (9) * 90, 0, 82, 36, 3, 0, RGB(255, 196, 0), RGB(238, 236, 255), true, 14, false, false
    //);
    Show_IMG::screen.cbs[14].init(
        1469, 84, 82, 32, 3, 0, RGB(255, 196, 0), RGB(238, 236, 255), true, 14, false, false
    );
    Show_IMG::screen.cbs[15].init
    (
        1560, 84, 100, 32, 2, 3, RGB(255, 129, 69), RGB(210, 200, 255), true, 15, true, false
    );
    Show_IMG::screen.cbs[16].init
    (
        9 * 90, 0, 82, 36, 3, 0, RGB(255, 129, 69), RGB(210, 200, 255), true, 16, false, false, RGB(96, 0, 96)
    );
    Show_IMG::screen.cbs[17].init
    (
        1469, 124, 86, 32, 3, 0, RGB(255, 129, 69), RGB(210, 200, 255), true, 17, false, false
    );
    Show_IMG::screen.cbs[18].init
    (
        1560, 124, 86, 32, 3, 0, RGB(255, 129, 69), RGB(210, 200, 255), true, 18, false, false
    );
    Show_IMG::screen.cbs[19].init
    (
        10 * 90, 0, 82, 36, 3, 0, RGB(255, 129, 69), RGB(210, 200, 255), true, 19, false, false, RGB(96, 0, 96)
    );
    Show_IMG::screen.cbs[20].init
    (
        11 * 90, 0, 82, 36, 3, 0, RGB(255, 129, 69), RGB(210, 200, 255), true, 20, false, false, RGB(96, 0, 96)
    );
    Show_IMG::screen.cbs[21].init
    (
        12 * 90, 0, 82, 36, 3, 0, RGB(255, 129, 69), RGB(210, 200, 255), true, 21, false, false, RGB(96, 0, 96)
    );
    Show_IMG::screen.cbs[0].s.set(L"打开文件");
    Show_IMG::screen.cbs[1].s.set(L"放大");
    Show_IMG::screen.cbs[2].s.set(L"缩小");
    Show_IMG::screen.cbs[3].s.set(L"水平翻转");
    Show_IMG::screen.cbs[4].s.set(L"垂直翻转");
    Show_IMG::screen.cbs[5].s.set(L"退出");
    Show_IMG::screen.cbs[7].s.set(L"上移");
    Show_IMG::screen.cbs[8].s.set(L"下移");
    Show_IMG::screen.cbs[9].s.set(L"左移");
    Show_IMG::screen.cbs[10].s.set(L"右移");
    Show_IMG::screen.cbs[11].s.set(L"设置缩放比");
    Show_IMG::screen.cbs[12].s.set(L"0.1");
    Show_IMG::screen.cbs[13].s.set(L"字体");
    Show_IMG::screen.cbs[14].s.set(L"模糊化");
    Show_IMG::screen.cbs[15].s.set(L"5.6");
    Show_IMG::screen.cbs[16].s.set(L"还原");
    Show_IMG::screen.cbs[17].s.set(L"enc");
    Show_IMG::screen.cbs[18].s.set(L"dec");
    Show_IMG::screen.cbs[19].s.set(L"显示调试");
    Show_IMG::screen.cbs[20].s.set(L"图片信息");
    Show_IMG::screen.cbs[21].s.set(L"保存图片");
    //
    Show_IMG::screen.cbs[0].OnClick = Show_IMG::Open;
    Show_IMG::screen.cbs[1].OnClick = Show_IMG::Amplify;
    Show_IMG::screen.cbs[2].OnClick = Show_IMG::Reduce;
    Show_IMG::screen.cbs[3].OnClick = Show_IMG::XFlip;
    Show_IMG::screen.cbs[4].OnClick = Show_IMG::YFlip;
    Show_IMG::screen.cbs[5].OnClick = Show_IMG::ExitMainloop;
    Show_IMG::screen.cbs[7].OnClick = Show_IMG::Up;
    Show_IMG::screen.cbs[8].OnClick = Show_IMG::Down;
    Show_IMG::screen.cbs[9].OnClick = Show_IMG::Left;
    Show_IMG::screen.cbs[10].OnClick = Show_IMG::Right;
    Show_IMG::screen.cbs[11].OnClick = Show_IMG::SetZoomRate;
    Show_IMG::screen.cbs[13].OnClick = Show_IMG::inter_setfont;
    Show_IMG::screen.cbs[14].OnClick = Show_IMG::RawSmooth;
    Show_IMG::screen.cbs[16].OnClick = Show_IMG::BackImg;
    Show_IMG::screen.cbs[17].OnClick = Show_IMG::Enc;
    Show_IMG::screen.cbs[18].OnClick = Show_IMG::Dec;
    Show_IMG::screen.cbs[19].OnClick = Show_IMG::SwitchDebug;
    Show_IMG::screen.cbs[20].OnClick = Show_IMG::ShowImageInfo;
    Show_IMG::screen.cbs[21].OnClick = Show_IMG::Save;
    //
    Show_IMG::screen.cbs[6].init
    (0, 52, 460, 880, 2, 1, RGB(255, 0, 255), RGB(255, 255, 255), false, 6, false, false);
    Show_IMG::screen.cbs[6].render = Show_IMG::render_img;
    //Show_IMG::screen.ShowDebugInfo = true;
    Show_IMG::screen.mainloop();
    return 0;
}
int Show_IMG::w_main(String &file)
{/*
    for (int i = 0; i < 6; i++)
    {
        Show_IMG::screen.cbs[i].init
        (
            i * 90, 0, 100, 48, 2, 0, RGB(255, 255, 0), RGB(220, 220, 255), true, i, false, false
        );
    }
    Show_IMG::screen.cbs[0].s.set(L"打开文件");
    Show_IMG::screen.cbs[1].s.set(L"放大");
    Show_IMG::screen.cbs[2].s.set(L"缩小");
    Show_IMG::screen.cbs[3].s.set(L"水平翻转");
    Show_IMG::screen.cbs[4].s.set(L"垂直翻转");
    Show_IMG::screen.cbs[5].s.set(L"退出");
    Show_IMG::screen.cbs[0].OnClick = Show_IMG::Open;
    Show_IMG::screen.cbs[1].OnClick = Show_IMG::Amplify;
    Show_IMG::screen.cbs[2].OnClick = Show_IMG::Reduce;
    Show_IMG::screen.cbs[3].OnClick = Show_IMG::XFlip;
    Show_IMG::screen.cbs[4].OnClick = Show_IMG::YFlip;
    Show_IMG::screen.cbs[5].OnClick = Show_IMG::ExitMainloop;
    Show_IMG::screen.cbs[6].init
    (0, 52, 1660, 880, 2, 1, RGB(255, 0, 255), RGB(255, 255, 255), false, 6, false, false);
    Show_IMG::screen.cbs[6].render = Show_IMG::render_img;
    */
    Show_IMG::img.load(const_cast<wchar_t*>(file.get()), true);
    swprintf(Show_IMG::ImgPath, L"%s", file.get());
    if (Show_IMG::img.img.getheight() >= Height - 20  ||  Show_IMG::img.img.getwidth() >= Width - 5)
    {
        double zmrt = 0.9;
        while (Show_IMG::img.img.getheight() * zmrt >= Height - 20  ||  Show_IMG::img.img.getwidth() * zmrt >= Width - 5)
        {
            zmrt *= 0.9;
        }
        Show_IMG::img.scale(zmrt, true);
    }
    Show_IMG::Image_old.img = Show_IMG::img.img;
    //MessageBoxW(seg::MainHandle, file.get(), L"Image Viewer - AutoOpen", MB_OK);
    Show_IMG::HadOpened = true;
    Show_IMG::w_main();
    //Show_IMG::screen.mainloop();
    return 0;
}

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPWSTR cmdl, int nShow)
{
    seg::EasyX_Initialize(1660, 930, WT_IVVER);
    try
    {
        Show_IMG::Idle_Text.set(L"当前没有打开的图片。\n点击左上角选择文件打开");
        SetFontWS::inits();
        Show_IMG::HadOpened = false;
        Show_IMG::ImageInfo.set(L"None");
        swprintf(Show_IMG::ImgPath, L"%s", L"\0\0\0\0");
        const wchar_t *s = cmdl;
        String file;
        String sf;

        file.set(s);
        sf.set(L"");
        if (file.len() < 4)
        {
            // No File
            Show_IMG::w_main();
        }
        else
        {
            if ((file[0]) == wchar_t('"'))
            {
                sf = file.strip(L"\"");
                Show_IMG::w_main(sf);
            }
            else
            {
                Show_IMG::w_main(file);
            }
        }
    }
    catch (seg::Traceback::Exception & se)
    {
        seg::Traceback::errorscreen(se);
    }
    catch(const std::exception& e)
    {
        wchar_t errmsg[1024];
        swprintf(errmsg, L"%hs", e.what()); // mbstowcs();
        seg::Traceback::Exception se(L"System exception", L"Unknown", errmsg);
        seg::Traceback::errorscreen(se);
    }
    EndBatchDraw();
    closegraph();
    return 0;
}
